<template>
  <div class="app-container">
    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" :disabled="$hasBP('bnt.coach.add') === false"
                 @click="add">添 加</el-button>
    </div>

    <!-- 列表 -->
    <el-table v-loading="listLoading" :data="list" stripe border style="width: 100%;margin-top: 10px;">

      <el-table-column label="序号" width="70" align="center" fixed="left">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="cover" label="封面图" align="center">
        <template slot-scope="scope">
          <el-image :src="scope.row.cover" style="width: 100px; height: 100px"/>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="活动页地址" :show-overflow-tooltip="true"  align="center" />
      <el-table-column label="活动状态" align="center">
        <template slot-scope="scope">
          <el-switch :value="scope.row.status === 1" @change="switchStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="活动类型" align="center" prop="type" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.types===0" type="success">轮播图</el-tag>
          <el-tag v-else type="info">推荐图</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" width="160" />
      <el-table-column label="操作" align="center" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" title="修改" @click="edit(scope.row.id)" />
          <el-button type="danger" icon="el-icon-delete" size="mini" title="删除" @click="removeDataById(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination :current-page="page" :total="total" :page-size="limit"
                   style="padding: 30px 0; text-align: center;" layout="total, prev, pager, next, jumper"
                   @current-change="fetchData" />
  </div>
</template>

<script>
import api from '@/api/content/activity'

export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: null, // banner列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象

      createTimes: []
    }
  },

  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    console.log('list created......')
    this.fetchData()
  },

  // 生命周期函数：内存准备完毕，页面渲染成功
  mounted() {
    console.log('list mounted......')
  },

  methods: {
    // 加载banner列表数据
    fetchData(page = 1) {
      this.page = page
      // api.getSearchList(this.page, this.limit, this.searchObj).then(
      //   response => {
      //     console.log(response)
      //     console.log(response.data)
      //     // this.list = response.data.records
      //     this.total = response.data.total
      //     // 数据加载并绑定成功
      //     this.listLoading = false
      //   }
      // )
      api.getPageList(this.page, this.limit, this.searchObj).then(
        response => {
          console.log(response)
          console.log(response.data)
          this.list = response.data.records
          this.total = response.data.total
          // 数据加载并绑定成功
          this.listLoading = false
        }
      )
    },

    // 重置查询表单
    resetData() {
      console.log('重置查询表单')
      this.searchObj = {}
      this.createTimes = []
      this.fetchData()
    },

    // 根据id删除数据
    removeDataById(id) {
      // debugger
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // promise
        // 点击确定，远程调用ajax
        return api.removeById(id)
      }).then((response) => {
        this.fetchData(this.page)
        this.$message.success(response.message || '删除成功')
      }).catch(() => {
        this.$message.info('取消删除')
      })
    },

    // 教练是否在职
    switchStatus(row) {
      row.status = row.status === 1 ? 0 : 1
      api.updateStatus(row.id, row.status).then(response => {
        if (response.code) {
          this.$message.success(response.message || '操作成功')
          this.fetchData()
        }
      })
    },

    // 去添加
    add() {
      this.$router.push('/content/activityInfo')
    },
    // 去编辑
    edit(id) {
      this.$router.push({
        path: '/content/activityInfo',
        query: { id: id }
      })
    }
  }
}
</script>
